package com.a3dweijing.wetouch2.Activity;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.base.BaseActivity;
import com.a3dweijing.wetouch2.bean.CheckCode;
import com.a3dweijing.wetouch2.datainterface.Getdataimpl;
import com.google.gson.Gson;

/**
 * Created by Yxd on 2017/10/11.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private TextView nextBtn;
    private EditText inputPhoneEdt;
    private String TAG = "--";
    private Handler handler;
    private LinearLayout userlay;
    private LinearLayout threelogin;
    private TextView userlaytxt;

    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        getSupportActionBar().show();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        nextBtn = (TextView) findViewById(R.id.next_btn);
        inputPhoneEdt= (EditText) findViewById(R.id.input_phone_edt);
        userlay= (LinearLayout) findViewById(R.id.login_user_lay_layout);
        userlaytxt= (TextView) findViewById(R.id.login_user_lay_txt);
        userlaytxt.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        threelogin= (LinearLayout) findViewById(R.id.login_user_three_login);
        nextBtn.setAlpha(0.38f);
        nextBtn.setEnabled(false);
        inputPhoneEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                Log.e(TAG, "beforeTextChanged: " + s);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length()==11)
                {
                    nextBtn.setAlpha(1f);
                    nextBtn.setEnabled(true);
                    userlay.setVisibility(View.VISIBLE);
                    threelogin.setVisibility(View.GONE);
                }else if(s.length()>11) {
                    nextBtn.setAlpha(0.38f);
                    nextBtn.setEnabled(false);
                    Toast.makeText(getBaseContext(),"输入超出11位", Toast.LENGTH_SHORT).show();
                    threelogin.setVisibility(View.VISIBLE);
                    userlay.setVisibility(View.GONE);

                }else {
                    nextBtn.setAlpha(0.38f);
                    nextBtn.setEnabled(false);
                    threelogin.setVisibility(View.VISIBLE);
                    userlay.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }

        });
        handler=new Handler()
        {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what)
                {
                    case 100:
                        endPopupwindow();
                        CheckCode checkCode=new Gson().fromJson(msg.obj.toString(),CheckCode.class);
                        if (checkCode.isFlag()){
                            Intent intent=new Intent(LoginActivity.this,SetPasswordActivity.class);
                            intent.putExtra("phone_number",inputPhoneEdt.getText().toString());
                            intent.putExtra("code",checkCode.getCode());
                            startActivity(intent);
                            finish();
                        }else {
                            Intent intent=new Intent(LoginActivity.this,PasswordLoginActivity.class);
                            intent.putExtra("phone_number",inputPhoneEdt.getText().toString());
                            startActivity(intent);
                            finish();
                        }
                        break;
                    case 404:
                        endPopupwindow();
                        Toast.makeText(getBaseContext(),msg.obj.toString(), Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };
    }





    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.next_btn:
                inputPhoneEdt.setEnabled(false);
                new GetUsermessageThread(inputPhoneEdt.getText().toString()).start();
                startPopupwindow(getBaseContext(),"正在检查手机号码...");
                break;
        }
    }
    private class GetUsermessageThread extends Thread
    {
        String phone_number;
        public GetUsermessageThread(String phone_number) {
            this.phone_number = phone_number;
        }

        @Override
        public void run() {
            super.run();
            String resulet=new Getdataimpl().remotUserRegistersMsg(phone_number);
            if (resulet!=null && resulet.length()>7)
            {
                handler.sendMessage(handler.obtainMessage(100,resulet));
            }else {
                handler.sendMessage(handler.obtainMessage(404,"网络繁忙，请稍后再试"));
            }
        }
    }

}
